
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;

/**
 * @author 绛河
 * 注册
 */
public class Enroll {
    DatabaseConnection dc = new DatabaseConnection();
    Font font = new Font("仿宋", Font.BOLD, 20);
    JFrame page1;
    JLabel textIcon1, textIcon2, textIcon3, textIcon4;
    JTextField entryField1, entryField3, entryField4;
    JPasswordField entryField2;
    JButton button1;

    public void studentSignIn() {
        page1 = new JFrame("学生注册页面");
        page1.setSize(600, 500);
        page1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page1.setLocationRelativeTo(null);
        page1.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon1 = new JLabel("账 号：");
        textIcon1.setBounds(180, 90, 100, 40);
        textIcon1.setFont(font);
        entryField1 = new JTextField(null, 50);
        entryField1.setBounds(280, 90, 150, 40);
        entryField1.setFont(font);
        textIcon2 = new JLabel("密 码：");
        textIcon2.setBounds(180, 140, 100, 40);
        textIcon2.setFont(font);
        entryField2 = new JPasswordField(null, 50);
        entryField2.setBounds(280, 140, 150, 40);
        entryField2.setEchoChar('*');
        entryField2.setFont(font);
        textIcon3 = new JLabel("姓 名：");
        textIcon3.setBounds(180, 190, 100, 40);
        textIcon3.setFont(font);
        entryField3 = new JTextField(null, 50);
        entryField3.setBounds(280, 190, 150, 40);
        entryField3.setFont(font);
        textIcon4 = new JLabel("班 级：");
        textIcon4.setBounds(180, 240, 100, 40);
        textIcon4.setFont(font);
        entryField4 = new JTextField(null, 50);
        entryField4.setBounds(280, 240, 150, 40);
        entryField4.setFont(font);

        button1 = new JButton("保存");
        button1.setBounds(240, 340, 80, 40);
        button1.setFont(font);
        page1.add(textIcon1);
        page1.add(textIcon2);
        page1.add(entryField1);
        page1.add(entryField2);
        page1.add(textIcon3);
        page1.add(entryField3);
        page1.add(textIcon4);
        page1.add(entryField4);
        page1.add(button1);
        page1.setVisible(true);
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement st = null, st1 = null,st2 = null;
                ResultSet rs = null,rs2 = null;
                Connection con = null;
                try {
                    con = dc.getConnection();
                    //执行SQL语句
                    String sql = "insert into student_message(student_number,student_name,class_number,password) values(?,?,?,?)";
                    String sql2 = "select * from student_message";
                    String sql3 = "select * from class";
                    st = con.prepareStatement(sql);
                    st1 = con.prepareStatement(sql2);
                    st2 = con.prepareStatement(sql3);
                    rs = st1.executeQuery();
                    rs2 = st2.executeQuery();
                    if (entryField1.getText().length() != 0) {
                        st.setString(1, entryField1.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "账号不能为空！");//弹出窗口
                    }
                    if (entryField2.getText().length() != 0) {
                        st.setString(4, entryField2.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "密码不能为空！");//弹出窗口
                    }

                    if (entryField3.getText().length() != 0) {
                        st.setString(2, entryField3.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "姓名不能为空！");//弹出窗口
                    }
                    if (entryField4.getText().length() != 0) {
                        st.setString(3, entryField4.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "班级不能为空！");//弹出窗口
                    }
                    while (rs.next()) {
                        if (rs.getString(1).equals(entryField1.getText())) {
                            JOptionPane.showMessageDialog(null, "该学生已经注册");
                            entryField1.setText(null);
                            entryField2.setText(null);
                            entryField3.setText(null);
                            entryField4.setText(null);
                            con.close();
                        }
                    }
                    boolean flag = false;
                    while (rs2.next()){
                        if (rs2.getString(1).equals(entryField4.getText())){
                            flag = true;
                        }
                    }
                    if(flag == false){
                        if (rs.getString(1).equals(entryField1.getText())) {
                            JOptionPane.showMessageDialog(null, "班级不存在");
                            entryField1.setText(null);
                            entryField2.setText(null);
                            entryField3.setText(null);
                            entryField4.setText(null);
                            con.close();
                        }
                    }
                    if (entryField1.getText().length() != 6) {
                        JOptionPane.showMessageDialog(null, "帐号为六位数字");
                        entryField1.setText(null);
                        entryField2.setText(null);
                        entryField3.setText(null);
                        entryField4.setText(null);
                        con.close();
                    }
                    if (entryField2.getText().length() != 6) {
                        JOptionPane.showMessageDialog(null, "密码为六位");
                        entryField1.setText(null);
                        entryField2.setText(null);
                        entryField3.setText(null);
                        entryField4.setText(null);
                        con.close();
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (entryField1.getText().length() != 0 && entryField2.getText().length() != 0 && entryField3.getText().length() != 0 && entryField3.getText().length() != 0) {
                    JOptionPane.showMessageDialog(null, "添加成功！");
                    entryField1.setText(null);
                    entryField2.setText(null);
                    entryField3.setText(null);
                    entryField4.setText(null);
                    page1.dispose();
                    new Register();
                    try {
                        st.executeUpdate();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        page1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new Register();
            }
        });
    }

    JFrame page2;
    JLabel textIcon5, textIcon6, textIcon7, textIcon8;
    JTextField entryField5, entryField7, entryField8;
    JPasswordField entryField6;
    JButton button2;

    public void teacherSignIn() {
        page2 = new JFrame("教师注册页面");
        page2.setSize(600, 500);
        page2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page2.setLocationRelativeTo(null);
        page2.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon5 = new JLabel("账 号：");
        textIcon5.setBounds(180, 90, 100, 40);
        textIcon5.setFont(font);
        entryField5 = new JTextField(null, 50);
        entryField5.setBounds(280, 90, 150, 40);
        entryField5.setFont(font);
        textIcon6 = new JLabel("密 码：");
        textIcon6.setBounds(180, 140, 100, 40);
        textIcon6.setFont(font);
        entryField6 = new JPasswordField(null, 50);
        entryField6.setBounds(280, 140, 150, 40);
        entryField6.setEchoChar('*');
        entryField6.setFont(font);
        textIcon7 = new JLabel("姓 名：");
        textIcon7.setBounds(180, 190, 100, 40);
        textIcon7.setFont(font);
        entryField7 = new JTextField(null, 50);
        entryField7.setBounds(280, 190, 150, 40);
        entryField7.setFont(font);
        textIcon8 = new JLabel("教授课程：");
        textIcon8.setBounds(150, 240, 120, 40);
        textIcon8.setFont(font);
        entryField8 = new JTextField(null, 50);
        entryField8.setBounds(280, 240, 150, 40);
        entryField8.setFont(font);

        button2 = new JButton("保存");
        button2.setBounds(240, 340, 80, 40);
        button2.setFont(font);

        page2.add(textIcon5);
        page2.add(textIcon6);
        page2.add(entryField5);
        page2.add(entryField6);
        page2.add(textIcon7);
        page2.add(entryField7);
        page2.add(textIcon8);
        page2.add(entryField8);
        page2.add(button2);
        page2.setVisible(true);
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement st = null, st1 = null,st2=null;
                ResultSet rs = null,rs2=null;
                Connection con = null;
                //注册驱动
                try {
                    con = dc.getConnection();
                    String sql = "insert into teacher_message(job_number,name,teacher_sex,password) values(?,?,?,?)";
                    String sql1 = "select * from teacher_message";
                    String sql2 = "select * from course";
                    st = con.prepareStatement(sql);
                    st1 = con.prepareStatement(sql1);
                    st2 = con.prepareStatement(sql2);
                    rs = st1.executeQuery();
                    rs2 = st2.executeQuery();
                    if (entryField5.getText().length() != 0) {
                        //st.setString(1, entryField5.getText());
                        st.setString(1, entryField5.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "账号不能为空！");//弹出窗口
                    }
                    if (entryField6.getText().length() != 0) {
                        st.setString(4, entryField6.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "密码不能为空！");//弹出窗口
                    }
                    if (entryField7.getText().length() != 0) {
                        st.setString(2, entryField7.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "姓名不能为空！");//弹出窗口
                    }
                    if (entryField8.getText().length() != 0) {
                        st.setString(3, entryField8.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "性别不能为空！");//弹出窗口
                    }
                    while (rs.next()) {
                        if (rs.getString(1).equals(entryField5.getText())) {
                            JOptionPane.showMessageDialog(null, "该老师已经注册");
                            entryField5.setText(null);
                            entryField6.setText(null);
                            entryField7.setText(null);
                            entryField8.setText(null);
                            con.close();
                        }
                    }
                    boolean flag = false;
                    while (rs2.next()){
                        if(rs2.getString(2).equals(entryField8.getText())){
                            flag = true;
                        }
                    }
                    if(flag==false){
                        JOptionPane.showMessageDialog(null, "该校没有该课程");
                        entryField5.setText(null);
                        entryField6.setText(null);
                        entryField7.setText(null);
                        entryField8.setText(null);
                        con.close();
                    }
                    if (entryField5.getText().length() != 3) {
                        JOptionPane.showMessageDialog(null, "账号为三位数字");
                        entryField5.setText(null);
                        entryField6.setText(null);
                        entryField7.setText(null);
                        entryField8.setText(null);
                        con.close();
                    }
                    if (entryField6.getText().length() != 6) {
                        JOptionPane.showMessageDialog(null, "密码为六位");
                        entryField5.setText(null);
                        entryField6.setText(null);
                        entryField7.setText(null);
                        entryField8.setText(null);
                        con.close();
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (entryField5.getText().length() != 0 && entryField6.getText().length() != 0 && entryField7.getText().length() != 0 && entryField8.getText().length() != 0) {
                    JOptionPane.showMessageDialog(null, "添加成功！");
                    entryField5.setText(null);
                    entryField6.setText(null);
                    entryField7.setText(null);
                    entryField8.setText(null);
                    page2.dispose();
                    new Register();
                    try {
                        st.executeUpdate();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        page2.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new Register();
            }
        });
    }

    JFrame page3;
    JLabel textIcon12,textIcon13,textIcon14;
    JTextField entryField9,entryField10;
    JPasswordField entryField11;
    JButton button3;

    public void administratorRegistration(){
        page3 = new JFrame("管理员注册页面");
        page3.setSize(600, 470);
        page3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page3.setLocationRelativeTo(null);
        page3.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon12 = new JLabel("账 号：");
        textIcon12.setBounds(180, 90, 100, 40);
        textIcon12.setFont(font);
        entryField9 = new JTextField(null, 50);
        entryField9.setBounds(280, 90, 150, 40);
        entryField9.setFont(font);
        textIcon13 = new JLabel("密 码：");
        textIcon13.setBounds(180, 140, 100, 40);
        textIcon13.setFont(font);
        entryField11 = new JPasswordField(null, 50);
        entryField11.setBounds(280, 140, 150, 40);
        entryField11.setEchoChar('*');
        entryField11.setFont(font);
        textIcon14 = new JLabel("姓 名：");
        textIcon14.setBounds(180, 190, 100, 40);
        textIcon14.setFont(font);
        entryField10 = new JTextField(null, 50);
        entryField10.setBounds(280, 190, 150, 40);
        entryField10.setFont(font);
        button3 = new JButton("保存");
        button3.setBounds(240, 340, 80, 40);
        button3.setFont(font);
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement st = null, st1 = null,st2=null;
                ResultSet rs = null;
                Connection con = null;
                //注册驱动
                try {
                    con = dc.getConnection();
                    String sql = "insert into administrator(administrator_number,administrator_name,password) values(?,?,?)";
                    String sql1 = "select * from administrator";
                    st = con.prepareStatement(sql);
                    st1 = con.prepareStatement(sql1);
                    rs = st1.executeQuery();
                    if (entryField9.getText().length() != 0) {
                        st.setString(1, entryField9.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "账号不能为空！");//弹出窗口
                    }
                    if (entryField11.getText().length() != 0) {
                        st.setString(3, entryField11.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "密码不能为空！");//弹出窗口
                    }
                    if (entryField10.getText().length() != 0) {
                        st.setString(2, entryField10.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "姓名不能为空！");//弹出窗口
                    }

                    while (rs.next()) {
                        if (rs.getString(1).equals(entryField9.getText())) {
                            JOptionPane.showMessageDialog(null, "该管理员已经注册");
                            entryField9.setText(null);
                            entryField10.setText(null);
                            entryField11.setText(null);
                            con.close();
                        }
                    }

                    if (entryField9.getText().length() != 3) {
                        JOptionPane.showMessageDialog(null, "账号为三位数字");
                        entryField9.setText(null);
                        entryField10.setText(null);
                        entryField11.setText(null);
                        con.close();
                    }
                    if (entryField11.getText().length() != 6) {
                        JOptionPane.showMessageDialog(null, "密码为六位");
                        entryField9.setText(null);
                        entryField10.setText(null);
                        entryField11.setText(null);
                        con.close();
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (entryField9.getText().length() != 0 && entryField10.getText().length() != 0 && entryField11.getText().length() != 0) {
                    JOptionPane.showMessageDialog(null, "添加成功！");
                    entryField9.setText(null);
                    entryField10.setText(null);
                    entryField11.setText(null);
                    page3.dispose();
                    new Register();
                    try {
                        st.executeUpdate();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        page3.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new Register();
                page3.dispose();
            }
        });

        page3.add(textIcon12);
        page3.add(textIcon13);
        page3.add(entryField9);
        page3.add(entryField10);
        page3.add(textIcon14);
        page3.add(entryField11);
        page3.add(button3);
        page3.setVisible(true);
    }
}
